package com.ry.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ry.novel.enums.ResponseStatus;
import com.ry.novel.exception.BusinessException;
import com.ry.novel.mapper.CategoryMapper;
import com.ry.novel.model.dto.BookInfo;
import com.ry.novel.model.dto.CategoryInfo;
import com.ry.novel.model.dto.condition.CategoryQuery;
import com.ry.novel.model.entity.Category;
import com.ry.novel.service.BookService;
import com.ry.novel.service.CategoryService;
import com.ry.novel.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ry
 * @since 2022-04-09 16:53
 **/
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;


    @Autowired
    private BookService bookService;

    @Override
    public Page<CategoryInfo> queryCategory(CategoryQuery categoryQuery) {
        Page<Category> page = new Page<>(categoryQuery.getPage(), categoryQuery.getSize());
        Page<CategoryInfo> infoPage = new Page<>(categoryQuery.getPage(), categoryQuery.getSize());

        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        if (categoryQuery.getName() != null){
            wrapper.eq("name",categoryQuery.getName());
        }
        categoryMapper.selectPage(page, wrapper);
        List<CategoryInfo> infos = page.getRecords().stream().map(entity -> getCategoryInfo(entity)).collect(Collectors.toList());

        infoPage.setRecords(infos);
        infoPage.setTotal(page.getTotal());


        return infoPage;
    }

    @Override
    public void addCategory(CategoryInfo info) {
        Category category = new Category();
        category.setName(info.getName());
        category.setCreateTime(LocalDateTime.now().toString());
        category.setUpdateTime(LocalDateTime.now().toString());

        categoryMapper.insert(category);
    }

    @Override
    public void updateCategory(CategoryInfo info) {
        Category category = new Category();
        category.setId(info.getId());
        category.setName(info.getName());
        category.setUpdateTime(LocalDateTime.now().toString());


        //还要找出book来修改分类名

        categoryMapper.updateById(category);
    }

    @Override
    public void deleteCategory(Long id) {
        List<BookInfo> bookInfos = bookService.queryByBookId(id);
        if (bookInfos == null || bookInfos.isEmpty()){
            throw new BusinessException(ResponseStatus.CATEGORY_DELETE_ERROR);
        }
        categoryMapper.deleteById(id);
    }

    @Override
    public List<CategoryInfo> queryAllCategory() {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        List<Category> categories = categoryMapper.selectList(wrapper);
        List<CategoryInfo> infos = categories.stream().map(this::getCategoryInfo).collect(Collectors.toList());

        return infos;
    }

    private CategoryInfo getCategoryInfo(Category entity) {
        CategoryInfo categoryInfo = new CategoryInfo();
        categoryInfo.setId(entity.getId());
        categoryInfo.setName(entity.getName());
        categoryInfo.setCreateTime(DateUtil.format(entity.getCreateTime()));
        categoryInfo.setUpdateTime(DateUtil.format(entity.getUpdateTime()));

        return categoryInfo;
    }
}
